﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using CMTVision.Widgets.Common;

namespace CMTVision.Widgets
{
    public static class SortableExtension
    {
        public static void InitSortable(this ISortable sortableObj, System.Web.UI.Control sortableControl)
        { 
            var sortableScriptBuilder = new StringBuilder();
             
            sortableScriptBuilder.AppendFormat("$('#{0}').addClass('{1}').sortable({2}).sortable({3});",
                                                sortableControl.ClientID,
                                                Constants.UI_ColumnHolder_ClassName,
                                                String.IsNullOrEmpty(sortableObj.SortableOptions) ? String.Empty : sortableObj.SortableOptions,
                                                "{connectWith:'." + Constants.UI_ColumnHolder_ClassName + "'}");

            sortableControl.RegisterScript(sortableControl.ClientID + "_Sortable", sortableScriptBuilder.ToString());
        }
    }

    public static class ControlExtension
    {
        public static void RegisterScript(this System.Web.UI.Control control, string key, string scriptContent)
        {
            System.Web.UI.HtmlControls.HtmlGenericControl scriptControl = new System.Web.UI.HtmlControls.HtmlGenericControl("script");
            scriptControl.Attributes.Add("id", key);
            scriptControl.Attributes.Add("type", "text/javascript");
            scriptControl.InnerHtml = scriptContent;
            control.Controls.Add(scriptControl);
        }
    }
}